package com.app.couponsharelife.ui.main.me.experiencemen

import android.content.Intent
import android.view.View
import com.google.gson.Gson
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.CurriculumPayBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.utils.AppUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_experience_curriculum.*
import kotlinx.android.synthetic.main.base_title_white.*
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *Created by xct on 2019/2/13
 *Description:体验课程选择页面
 */
class ExperienceCurriculumActivity:BaseActivity() {
    private var payType=""
    override val viewLayoutId: Int
        get() = R.layout.activity_experience_curriculum

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        basetitle_llclose.setOnClickListener { finish() }
        basetitle_title.text="购买课程"
        experience_curriculum_buts.setOnClickListener {
            if (payType!="1"){
                startIntent("1")
            }
        }
        experience_curriculum_butx.setOnClickListener {
            if (payType!="2"){
                startIntent("2")
            }
        }
        getHHRData()
    }
    private fun startIntent(type:String){
        val intent=Intent(this,ExperienceMenPayActivity::class.java)
        intent.putExtra("Type","Curriculum")
        intent.putExtra("Data",type)
        intent.putExtra("Data1",payType)
        startActivity(intent)
    }
    private fun setData(type:String){
        payType=type
        when(type){
            "1"->{  //上购买
                experience_curriculum_kcslogo.setImageResource(R.drawable.tygjj_xz_kcs_y)
                experience_curriculum_kcstext.setTextColor(resources.getColor(R.color.golden_ffd))
                experience_curriculum_kcstext1.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_kcsrmb.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_kcsprice.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_ygms.visibility=View.VISIBLE
                experience_curriculum_buts.setBackgroundResource(R.drawable.shape_gray_fdf50)
                experience_curriculum_buts.text="已购买"
            }
            "2"->{  //下购买
                experience_curriculum_kcxlogo.setImageResource(R.drawable.tygjj_xz_kcx_y)
                experience_curriculum_kcxtext.setTextColor(resources.getColor(R.color.golden_ffd))
                experience_curriculum_kcxtext1.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_kcxrmb.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_kcxprice.setTextColor(resources.getColor(R.color.golden_fee))
                experience_curriculum_ygmx.visibility=View.VISIBLE
                experience_curriculum_butx.setBackgroundResource(R.drawable.shape_gray_fdf50)
                experience_curriculum_butx.text="已购买"
            }
            else->{  //未购买

            }
        }
    }
    private fun getHHRData(){
        showProgressDialog("加载中...")
        val map= HashMap<String,String>()
        map["userId"]=AppUtils.getString(Contacts.USERID,"")
        map["timestamp"] = System.currentTimeMillis().toString()
        map["sign"] = AppUtils.getMapString(map)
        RetrofitWorker.retrofitWorker.getHHRStauts(RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), Gson().toJson(map)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object :DisposableObserver<RemoteResultBean<List<CurriculumPayBean>>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<List<CurriculumPayBean>>) {
                        dismissProgressDialog()
                        if (it.data!=null&&it.data!!.isNotEmpty()){
                            val curriculumPayBean=it.data!![0]
                            setData(curriculumPayBean.updown!!)
                        }else{
                            setData("0")
                        }
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }
                })
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        getHHRData()
    }
    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}